# -*- coding: utf-8 -*-
"""
# 使用了 [openpyxl==3.1.2]，遵循其 [MIT] 许可证，原始代码来源：[https://openpyxl.readthedocs.io]
# 使用了 [pandas==2.0.3]，遵循其 [BSD 3-Clause License] 许可证，原始代码来源：[https://pandas.pydata.org]
"""
import pandas as pd
import datetime
import os
import re
import sys
from openpyxl.styles import Alignment
from openpyxl.styles import Font
from openpyxl.styles.borders import Border,Side

def create_temp_df():
    df=pd.DataFrame(columns=["Reference Type","Title","Author","Author Address","Journal","Year",
                             "Issue","Pages","Keywords","Abstract","ISBN/ISSN","Notes","URL",
                             "DOI","Database Provider"])
    return(df)

thin_border=Border(left=Side(style='thin'),
                   right=Side(style='thin'),
                   top=Side(style='thin'),
                   bottom=Side(style='thin'))

def save_dataframe(df,prefix=""):
    ticks=datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
    if not os.path.exists(r"./result"):
        os.makedirs(r"./result")
    if(prefix!=""):
        prefix+="_"
    fn=r"./result/"+prefix+re.split(r'[_.]',os.path.basename(sys.argv[0]))[1]+"_"+ticks+".xlsx"
    with pd.ExcelWriter(fn,engine='openpyxl') as writer:
        df.to_excel(writer,index=False)
        workbook = writer.book
        worksheet = writer.sheets['Sheet1']
        worksheet.freeze_panes = 'A2'
        for row in worksheet.iter_rows(min_row=1, max_col=worksheet.max_column, max_row=worksheet.max_row):
            for cell in row:
                cell.alignment=Alignment(horizontal='center', vertical='center', wrapText=True)
                cell.font=Font(name="宋体",sz=9)
                cell.border = thin_border
        for sheet in workbook:
            max_row = sheet.max_row
            for row in range(1, max_row + 1):
                sheet.row_dimensions[row].height = 12
    return(fn)
def read_net(file_path):
    df=create_temp_df()
    with open(file_path,encoding='utf-8', errors='ignore') as f:
        p01=""
        p02=""
        p03=""
        p04=""
        p05=""
        p06=""
        p07=""
        p08=""
        p09=""
        p10=""
        p11=""
        p12=""
        p13=""
        p14=""
        p15=""
        for line in f:
            if line.startswith("{Reference Type}:" ):
                p01+=re.sub("{Reference Type}:","",line)
                p01=p01.strip()
                continue
            if line.startswith("{Title}:" ):
                p02+=re.sub("{Title}:","",line)
                p02=p02.strip()
                continue
            if line.startswith("{Author}:" ):
                p03+=re.sub("{Author}:","",line)
                p03=p03.strip()
                continue
            if line.startswith("{Author Address}:" ):
                p04+=re.sub("{Author Address}:","",line)
                p04=p04.strip()
                continue
            if line.startswith("{Journal}:" ):
                p05+=re.sub("{Journal}:","",line)
                p05=p05.strip()
                continue
            if line.startswith("{Year}:" ):
                p06+=re.sub("{Year}:","",line)
                p06=p06.strip()
                continue
            if line.startswith("{Issue}:" ):
                p07+=re.sub("{Issue}:","",line)
                p07=p07.strip()
                continue
            if line.startswith("{Pages}:" ):
                p08+=re.sub("{Pages}:","",line)
                p08=p08.strip()
                continue
            if line.startswith("{Keywords}:" ):
                p09+=re.sub("{Keywords}:","",line)
                p09=p09.strip()
                continue
            if line.startswith("{Abstract}:" ):
                p10+=re.sub("{Abstract}:","",line)
                p10=p10.strip()
                continue
            if line.startswith("{ISBN/ISSN}:" ):
                p11+=re.sub("{ISBN/ISSN}:","",line)
                p11=p11.strip()
                continue
            if line.startswith("{Notes}:" ):
                p12+=re.sub("{Notes}:","",line)
                p12=p12.strip()
                continue
            if line.startswith("{URL}:" ):
                p13+=re.sub("{URL}:","",line)
                p13=p13.strip()
                continue
            if line.startswith("{DOI}:" ):
                p14+=re.sub("{DOI}:","",line)
                p14=p14.strip()
                continue
            if line.startswith("{Database Provider}:" ):
                p15+=re.sub("{Database Provider}:","",line)
                p15=p15.strip()
                dicts={"Reference Type":p01,\
                        "Title":p02,\
                        "Author":p03,\
                        "Author Address":p04,\
                        "Journal":p05,\
                        "Year":p06,\
                        "Issue":p07,\
                        "Pages":p08,\
                        "Keywords":p09,\
                        "Abstract":p10,\
                        "ISBN/ISSN":p11,\
                        "Notes":p12,\
                        "URL":p13,\
                        "DOI":p14,\
                        "Database Provider":p15}
                df_temp=pd.DataFrame.from_dict(dicts,orient='index').T
                df=pd.concat([df,df_temp],ignore_index=True)
                p01=""
                p02=""
                p03=""
                p04=""
                p05=""
                p06=""
                p07=""
                p08=""
                p09=""
                p10=""
                p11=""
                p12=""
                p13=""
                p14=""
                p15=""
        f.close()
    save_dataframe(df)


if __name__ == "__main__":
    import time
    old_time=time.time()
    fp=r"F:\Andrewz\B_Study\CNKI-20250713091010630.net"
    read_net(fp)
    current_time=time.time()
    print("运行时间为"+str(current_time-old_time)+"s")